/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jsf.beans;

import java.util.List;
import javax.persistence.Query;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Mateusz
 */
class UserDAO {
    
    public boolean insertData(Users p){
        EntityManagerFactory emf = Persistence.createEntityManagerFactory("JavaWebPU");
        List <Users> l;
        boolean registered = false;
        EntityManager em = emf.createEntityManager();
        try{
            Query q = em.createNamedQuery("Users.findAll");
            l= q.getResultList();
            for(Users el:l){
                if(el.getEmail().equals(p.getEmail()))
                    registered = true;
            }
            
        }catch(Exception e){
            e.printStackTrace();
        }
        if(!registered){
            em.getTransaction().begin();
            em.persist(p);
            em.getTransaction().commit();
            em.close();
                return true;
              
        }else{
        em.close();
        return false;
        }
    }
}
